package ckw.utils;

import java.util.Date;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import ckw.database.DatabaseService;
import ckw.database.model.Election;
import ckw.database.model.ElectionNode;
import ckw.database.model.Member;
import ckw.database.model.Node;
import ckw.database.model.Voter;
import ckw.election.ElectionService;
import ckw.manager.ManagerController;
import core.cards.election.model.ElectionBackend;
import core.database.NodeType;
import core.database.SexType;
import core.exceptions.ObjectUpdateException;
import core.utils.ConfigPropertiesHolder;
import core.utils.Converter;
import core.utils.Property;

@Service
public class DummyDataProvider implements ApplicationContextAware {

	private static final Logger L = LoggerFactory.getLogger(DummyDataProvider.class);

	private ApplicationContext context;

	private DatabaseService databaseService;

	private ManagerController managerController;

	private Node ckwNode;

	@Inject
	public DummyDataProvider(DatabaseService databaseService, ElectionService electionService) {
		L.debug("Tworzę DummyDataProvider");

		Assert.notNull(databaseService);
		Assert.notNull(electionService);

		this.databaseService = databaseService;

		if (ConfigPropertiesHolder.getInstance().getProperty(Property.HBM2DDL_AUTO).equals("create") && ConfigPropertiesHolder.getInstance().getProperty(Property.SAMPLE_DATA).equals("true"))
			setUpData();

		L.debug("Utworzono DummyDataProvider");
	}

	public void addSampleData() {
		try {
			managerController = context.getBean(ManagerController.class);

			Node rkw1Node = new Node("RKW 1", "127.0.0.1", "8881", "rkw1_address", NodeType.RKW, ckwNode);
			rkw1Node = managerController.saveNode(rkw1Node);

			Node rkw2Node = new Node("RKW 2", "127.0.0.1", "8882", "rkw2_address", NodeType.RKW, ckwNode);
			rkw2Node = managerController.saveNode(rkw2Node);

			Node okw1Node = new Node("OKW 1", "127.0.0.1", "8883", "okw1_address", NodeType.OKW, ckwNode);
			okw1Node = managerController.saveNode(okw1Node);

			Node rkw3Node = new Node("RKW 3", "127.0.0.1", "8884", "rkw3_address", NodeType.RKW, okw1Node);
			rkw3Node = managerController.saveNode(rkw3Node);
			Node rkw4Node = new Node("RKW 4", "127.0.0.1", "8885", "rkw4_address", NodeType.RKW, okw1Node);
			rkw4Node = managerController.saveNode(rkw4Node);

			Node okw2Node = new Node("OKW 2", "127.0.0.1", "8886", "okw2_address", NodeType.OKW, ckwNode);
			okw2Node = managerController.saveNode(okw2Node);

			Node rkw5Node = new Node("RKW 5", "127.0.0.1", "8887", "rkw5_address", NodeType.RKW, okw2Node);
			rkw5Node = managerController.saveNode(rkw5Node);
			Node rkw6Node = new Node("RKW 6", "127.0.0.1", "8888", "rkw6_address", NodeType.RKW, okw2Node);
			rkw6Node = managerController.saveNode(rkw6Node);

			Member member1 = new Member("John", "White", "ul. Stoczniowców 74 75-256 Koszalin", "29112514289", SexType.MALE);

			member1 = managerController.storeMember(member1);
			Member member2 = new Member("Anita", "Red", "ul. Jaśminowa 91, 44-203 Rybnik", "64032804898", SexType.FEMALE);
			member2 = managerController.storeMember(member2);
			Member member3 = new Member("Mike", "Brown", "ul. Bajeczna 132 15-578 Białystok ", "88051307451", SexType.MALE);
			member3 = managerController.storeMember(member3);
			Member member4 = new Member("Kelly", "Pink", "ul. Baśniowa 68 02-349 Warszawa", "41120900513", SexType.FEMALE);
			member4 = managerController.storeMember(member4);
			Member member5 = new Member("Tom", "Green", "Al. Grzecznarowskiego Józefa 143 26-610 Radom", "34050502069", SexType.MALE);
			member5 = managerController.storeMember(member5);
			Member member6 = new Member("Rachel", "Blue", "ul. Baśniowa 68 02-349 Warszawa ", "24011715881", SexType.FEMALE);
			member6 = managerController.storeMember(member6);
			Member member7 = new Member("Hank", "Black", "ul. Dworcowa 109 63-702 Krotoszyn", "88082404518", SexType.MALE);
			member7 = managerController.storeMember(member7);
			Member member8 = new Member("Jill", "Brown", "ul. Kutnowska 120 61-046 Poznań", "63011607011", SexType.FEMALE);
			member8 = managerController.storeMember(member8);

			Voter voter1 = new Voter("Tom", "Apple", "ul. Mali Hilarego 78 25-630 Kielce", SexType.MALE, "98092116277", rkw1Node);
			voter1 = managerController.storeVoter(voter1);
			Voter voter2 = new Voter("Barbara", "Pear", "ul. Mali Hilarego 78 25-630 Kielce", SexType.FEMALE, "53022114598", rkw1Node);
			voter2 = managerController.storeVoter(voter2);
			Voter voter3 = new Voter("Hank", "Pineapple", "ul. Samopomocy 31 91-219 Łódź", SexType.MALE, "97072115475", rkw1Node);
			voter3 = managerController.storeVoter(voter3);
			Voter voter4 = new Voter("Trish", "Blueberry", "ul. Braci Wagów 18 02-791 Warszawa", SexType.FEMALE, "72012501095", rkw1Node);
			voter4 = managerController.storeVoter(voter4);
			Voter voter5 = new Voter("Tom", "Strawberry", "ul. Gruntowa 68 44-270 Rybnik", SexType.MALE, "29032019905", rkw3Node);
			voter5 = managerController.storeVoter(voter5);
			Voter voter6 = new Voter("Denise", "Peach", "ul. Cedrowa 147 61-449 Poznań", SexType.FEMALE, "95050914438", rkw3Node);
			voter6 = managerController.storeVoter(voter6);
			Voter voter7 = new Voter("Jake", "Potato", "ul. Leskiego Kazimierza 33 80-180 Gdańsk", SexType.MALE, "58070301536", rkw4Node);
			voter7 = managerController.storeVoter(voter7);
			Voter voter8 = new Voter("Hortensia", "Tomato", "ul. Szarotkowa 105 35-604 Rzeszów", SexType.FEMALE, "84031806431", rkw4Node);
			voter8 = managerController.storeVoter(voter8);

			Election election = new Election();
			election.setName("Wybory sejmowe 2018");
			election.setDescription("election_description");
			election.setStartDate(Converter.stringToDate("09.24.2018 06:00:00"));
			election.setEndDate(Converter.stringToDate("09.24.2018 22:00:00"));

			managerController.addNonResidentVoter(election, voter1);
			managerController.addNonResidentVoter(election, voter2);

			ElectionNode electionNode1 = new ElectionNode();
			electionNode1.setNode(rkw1Node);
			electionNode1.setElection(election);
			managerController.addMember(electionNode1, member1);

			ElectionNode electionNode2 = new ElectionNode();
			electionNode2.setNode(rkw4Node);
			electionNode2.setElection(election);
			managerController.addMember(electionNode2, member2);
			managerController.addMember(electionNode2, member3);

			managerController.addResidentVoter(election, voter2);

			ElectionBackend electionFormBackend = ElectionBackend.createDemoElectionFormBackend();
			election.setCards(Converter.objectToXML(electionFormBackend));

			election = managerController.saveElection(election);
		} catch (ObjectUpdateException e) {
			e.printStackTrace();
		}
	}

	public void setUpData() {
		ckwNode = new Node("CKW", "127.0.0.1", "8880", "ckw_address", NodeType.CKW, databaseService.getBaseNode());
		ckwNode = databaseService.storeNode(ckwNode);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.context = applicationContext;
	}
}
